use utf8;
use strict;

my %p; # pattern -> @words

binmode(STDIN, ":encoding(utf-8)");
binmode(STDOUT, ":encoding(utf-8)");
while (<STDIN>) {
  chomp $_;
  if ($_ =~ /^<([^>]+)>,\s+<([^>]+)>/) {
    my ($a, $b) = ($1, $2);
    find_pattern($a, $b);  
  }
}

foreach my $x (sort {scalar @{$p{$b}} <=> scalar @{$p{$a}}} keys %p) {
  print "PATTERN $x (" . scalar @{$p{$x}} . ")\n";
  foreach my $e (@{$p{$x}}) {
    print "\t$e\n";
  }
}

sub find_pattern {
  my ($w1, $w2) = @_;
  my $n = 0;
  my $min_len = length($w1) < length($w2) ? length($w1) : length($w2);
  my @a = split(//, $w1);
  my @b = split(//, $w2);
if ($min_len < 1) { die "$w1 $w2"; }
  for (; $n < $min_len; $n++) {
    if ($a[$n] ne $b[$n]) {
      last;
    }
  }
 
  my $sa = join("", @a[$n..length($w1)]); 
  my $sb = join("", @b[$n..length($w2)]);
  push @{$p{$sa."/".$sb}}, $w1 . " - " . $w2;
}
